package com.example.module_devices

import android.app.Activity
import android.app.TimePickerDialog
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.module_devices.databinding.ActivityAddDevicesBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import com.yuwei.base.BaseActivity
import com.yuwei.common.path.RouterPath
import com.yuwei.http.entity.DeviceEntityItem
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.ADD_DEVICES)
class AddDevicesActivity : BaseActivity<ActivityAddDevicesBinding,DeviceViewModel>() {
    override fun initView() {
        binding.deviceAddBack.setOnClickListener {
            finish()
        }
        val adapter = ArrayAdapter.createFromResource(
            this,
            R.array.spinner_device_items,
            android.R.layout.simple_spinner_item
        )
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.deviceSpType.adapter = adapter
        binding.deviceSpType.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: android.view.View?, position: Int, id: Long) {
                val selectedItem = parent.getItemAtPosition(position).toString()
                println("选中的项：$selectedItem")
                binding.deviceEtType.setText(selectedItem)
            }
            override fun onNothingSelected(parent: AdapterView<*>) {}
        }
        binding.btnSubmit.setOnClickListener {
            val deviceItem= DeviceEntityItem(
                id = 0,
                deviceName = binding.deviceEtName.text.toString(),
                deviceType = binding.deviceEtType.text.toString(),
                failureRate =binding.deviceEtLv.text.toString().toDouble(),
                installationLocation = binding.deviceEtAddress.text.toString(),
                status = binding.deviceEtType.text.toString()
            )
            vm.addDevice(deviceItem)
        }
        binding.deviceTimeView.setOnClickListener {
            TimePickerDialog(this, { _, hourOfDay, minute ->
                binding.deviceEtTime.text = "${hourOfDay}:${minute}"
            },0,0,true).show()
        }
        binding.deviceImage.setOnClickListener {
            PictureSelector.create(this, 512).selectPicture()
        }
    }

    override fun initData() {
        vm.addDeviceSuccess.observe(this){
            ToastUtils.showShort("添加成功")
            finish()
        }

    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 512 && resultCode == Activity.RESULT_OK) {
            val extras = data?.extras
            val pictureBean = extras?.get(PictureSelector.PICTURE_RESULT) as? PictureBean
            pictureBean?.path?.let { path ->
                binding.deviceImage2.visibility= View.VISIBLE
                Glide.with(this).load(path).into(binding.deviceImage2)
            }
        }

    }

}